import '../public/index'
import url from '../data/url'
import request from '../data/request'
import { tableConfig } from '../data/config'
import { getUrlParam } from '../data/util'
import store from 'store'

const base                   = url.base,
      getDeveloperManageList     = request.getDeveloperManageList;

let table,
    storeData,
    keyboardId = getUrlParam('keyboardId')

// 初始化表格
table = $('#table').bootstrapTable($.extend({}, tableConfig, {
  url: base + getDeveloperManageList,
  queryParams: function (params) {
    return $.extend({
      currentPage : params.pageNumber,
      pageSize    : params.pageSize,
  },$('#searchForm').serializeObject())
  },
  columns: [
    {
      field: 'systemUser.loginName',
      title: '用户名',
      valign: 'middle'
    }, {
      field: 'enterInfo.enterprisePrefix',
      title: '企业代码',
      valign: 'middle'
    }, {
      field: 'enterInfo.enterpriseName',
      title: '开发商名称',
      valign: 'middle'
    }, {
      field: 'systemUser.realName',
      title: '管理员姓名',
      valign: 'middle'
    }, {
      field: 'bankName',
      title: '结算开户行',
      valign: 'middle'
    }, {
      field: 'invitationCode',
      title: '邀请码',
      valign: 'middle'
    }, {
      field: 'action',
      valign: 'middle',
      align: 'center',
      title: '操作',
      formatter: function(index, row) {
        return `<div class="btn-group">
                      <button type="button" data-id="${ row.id }" class="btn btn-default btn-modify">修改</button>                  
                      <button type="button" data-id="${ row.id }" class="btn btn-default btn-detail">详情</button>                  
                </div>`
      }
    }]
}))

// 点击列表的时候传递数据到全局变量
table.on('click-row.bs.table', (e, data, el) => {
  storeData = data
})

// 搜索
$('#search-btn').on('click', (e) => {
  e.preventDefault();

  table.bootstrapTable('refresh', {
    query: $.extend({
      currentPage : 1,
    }, $('#searchForm').serializeObject())
  })
})

//跳转至修改页
$(document).on('click', '#table .btn-modify', (e) => {
  e.preventDefault()

  console.log('修改')

  store.set('storeData', storeData)
  window.location.href = `./modify.html?id=${ storeData.id }`
})

//跳转至详情页
$(document).on('click', '#table .btn-detail', (e) => {
  e.preventDefault()

  console.log('详情')
  store.set('storeData', storeData)
  window.location.href = `./detail.html?id=${ storeData.id }`
})